﻿#region Personnal Using
using FlanProjectData.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using FlanProjectData.Helpers;
#endregion

namespace FlanProject.ScreenMenu
{
    public class MainMenuScreen : InterfaceScreen
    {
        private static Logger logger = new Logger("MainMenuScreen");

        // List screen who can be accessible with this screen
        protected new enum AccessibleForm
        {
            NEWGAME,
            LOADGAME,
            OPTION,
            CREDIT,
            EXITGAME,
        }

        #region attributs
        private float distanceBetween2Button = (float)0.12;	//Define the distance in % of 2 button

        private float widthDistanceButton = (float)0.36;	//Define the distance in % from the right-side & the button
        private float heightDistanceButton = (float)0.24;	//Define the distance in % from the top-side & the FIRST button

        private float heightPercentButton = (float)0.09;	//Define the height in % of the button
        private float widthPercentButton = (float)0.43;		//Define the width in % of the button

        private float widthDistanceText = (float)0.04;
        private float heightDistanceText = (float)0.14;

        private float policeSize = 1;
        #endregion

        #region Ctor
        public MainMenuScreen()
        {
            this.Visible = true;
        }
        #endregion

        #region Overrided HandleInput

        public override void HandleInput()
        {
            if (InputManager.IsActionTriggered(InputManager.Action.Up))
            {
                selectedIndex = (selectedIndex - 1 + lstMenuEntrySelectable.Count) % lstMenuEntrySelectable.Count;
                logger.Debug(selectedIndex.ToString());
            }
            else if (InputManager.IsActionTriggered(InputManager.Action.Down))
            {
                selectedIndex = Math.Abs(selectedIndex + 1) % lstMenuEntrySelectable.Count;
                logger.Debug(selectedIndex.ToString());
            }
            // Validation of selected index
            else if (InputManager.IsActionTriggered(InputManager.Action.Ok))
            {
                logger.Info("Validation on :" + selectedIndex.ToString());

                // Execute MenuEntryAction
                this.lstMenuEntrySelectable[selectedIndex].OnSelectEntry();
            }
            else if (InputManager.IsActionTriggered(InputManager.Action.Exit))
            {
                logger.Warn("Fermeture du programme");
                this.ExitScreen();
            }
        }

        #endregion

        #region Overrided LoadContent

        public override void LoadContent(ContentManager cm)
        {
            int i = 0;

            #region add buttons

            #region NewGameButton

            this.lstMenuEntrySelectable.Add(new MenuEntry("New Game",
                new Vector2(((float)windowsWidth * widthDistanceButton),
                    ((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
                new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
            this.lstMenuEntrySelectable[i - 1].Selected += this.NewGameScreen;
            #endregion

            #region LoadGameButton

            this.lstMenuEntrySelectable.Add(new MenuEntry("Load Game",
                new Vector2(((float)windowsWidth * widthDistanceButton),
                    ((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
                new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
            this.lstMenuEntrySelectable[i - 1].Selected += this.LoadingScreen;

            #endregion

            #region OptionButton

            this.lstMenuEntrySelectable.Add(new MenuEntry("Options",
                new Vector2(((float)windowsWidth * widthDistanceButton),
                    ((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
                new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
            this.lstMenuEntrySelectable[i - 1].Selected += this.OptionScreen;

            #endregion

            #region CreditButton

            this.lstMenuEntrySelectable.Add(new MenuEntry("Credits",
                new Vector2(((float)windowsWidth * widthDistanceButton),
                    ((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
                new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));
            this.lstMenuEntrySelectable[i - 1].Selected += this.CreditScreen;

            #endregion

            #region QuitGameButton
            this.lstMenuEntrySelectable.Add(new MenuEntry("Quit Game",
                new Vector2(((float)windowsWidth * widthDistanceButton),
                    ((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
                new Vector2((float)windowsWidth * widthPercentButton,
                    (float)windowsHeight * heightPercentButton), policeSize));

            this.lstMenuEntrySelectable[i - 1].Selected += this.ExitScreen;
            #endregion

            #region Test - LoadingGame
            /*
			this.lstMenuEntrySelectable.Add(new MenuEntry("Test - LoadingGame",
				new Vector2(((float)windowsWidth * widthDistanceButton),
					((float)windowsHeight * heightDistanceButton) + ((float)windowsHeight * (distanceBetween2Button * i++))),
				new Vector2((float)windowsWidth * widthPercentButton,
					(float)windowsHeight * heightPercentButton)));

			this.lstMenuEntrySelectable[i - 1].Selected += this.LoadingGame;
            */
            #endregion

            #endregion

            #region add text

            this.lstMenuEntryUnselectable.Add(new MenuEntry("Once upon a time ...",
                new Vector2((float)windowsWidth * widthDistanceText,
                    (float)windowsHeight * heightDistanceText),
                true, Color.Black, policeSize));

            #endregion

            this.background = cm.Load<Texture2D>(@"Sprites\Menus\commonBackgroundMenu");

            // calculate the scale of the background
            base.calcScaleBackground(windowsHeight, windowsWidth);

            // Use virtual definition
            base.LoadContent(cm);
        }

        #endregion

        #region Overrided Draw

        #endregion

        /*public override InterfaceManager ScreenIsChanging()
		{
			InterfaceManager im = null;
			if (this.OnSwitch)
			{
				//im = new
			}
			return im;
		}*/

        #region Method To Switch Windows

        private void LoadingScreen(AccessibleForm a)
        {
            switch (a)
            {
                case AccessibleForm.NEWGAME:
                    logger.Info("Loading New Game Screen");
                    this.imChild = new NewGameScreen();
                    break;
                case AccessibleForm.LOADGAME:
                    this.imChild = new LoadingScreen();
                    break;
                case AccessibleForm.OPTION:
                    logger.Info("Loading Option screen");
                    this.imChild = new OptionMenuScreen();
                    break;
                case AccessibleForm.CREDIT:
                    logger.Info("Loading Credit screen");
                    this.imChild = new CreditMenuScreen();
                    break;
                case AccessibleForm.EXITGAME:
                    break;
                default:
                    logger.Warn("AccessibleForm : " + a.ToString() + " not exist");
                    break;
            }
            this.imChild.LoadContent(this.Content);
            this.Visible = false;
            this.imChild.Visible = true;

        }

        public void NewGameScreen(object sender, EventArgs e)
        {
            this.LoadingScreen(AccessibleForm.NEWGAME);
        }

        public void CreditScreen(object sender, EventArgs e)
        {
            this.LoadingScreen(AccessibleForm.CREDIT);
        }



        public void OptionScreen(object sender, EventArgs e)
        {
            this.LoadingScreen(AccessibleForm.OPTION);
        }

        public void LoadingScreen(object sender, EventArgs e)
        {
            this.LoadingScreen(AccessibleForm.LOADGAME);
        }

        public void LoadingGame(object sender, EventArgs e)
        {
            this.IsLoadingGame = true;
        }



        #endregion
    }
}
